package megamek.common.actions;

import static org.junit.Assert.*;

import java.util.Vector;

import megamek.client.bot.TestBot;
import megamek.common.BipedMech;
import megamek.common.Board;
import megamek.common.Coords;
import megamek.common.Entity;
import megamek.common.GunEmplacement;
import megamek.common.Hex;
import megamek.common.MiscType;
import megamek.common.Mounted;
import megamek.common.Player;
import megamek.common.Tank;
import megamek.common.TargetRoll;
import megamek.common.ToHitData;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

// TODO: Auto-generated Javadoc
/**
 * The Class PushAttackActionTest.
 */
public class PushAttackActionTest
{

	/** The bot. */
	static TestBot bot;

	/** The attacker. */
	static Player attacker = new Player(1, "attacker");

	/** The target. */
	static Player target = new Player(2, "target");

	/** The attack action. */
	static PushAttackAction attackAction;

	/** The club. */
	static Mounted club;

	/** The equipment type. */
	static MiscType miscType;

	/** The target ent. */
	static Entity attackerEnt, targetEnt, mechEnt;

	/** The entities. */
	static Vector<Entity> entities;

	/**
	 * Sets the up before class.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{ // add the players to the game
		bot = new TestBot("game", "localhost", 111);
		bot.game.addPlayer(attacker.getId(), attacker);
		bot.game.addPlayer(target.getId(), target);

		// create the board and hexs to fill the board
		bot.game.setBoard(new Board(3, 3));
		for (int i = 0; i < bot.game.getBoard().getWidth(); i++)
		{
			for (int j = 0; j < bot.game.getBoard().getHeight(); j++)
			{
				bot.game.getBoard().setHex(new Coords(i, j), new Hex(0));
			}
		}

		// add the entities to the game
		attackerEnt = new GunEmplacement();
		attackerEnt.setOwner(attacker);
		attackerEnt.setId(15);
		attackerEnt.setPosition(new Coords(0, 0));
		attackerEnt.setWeight(200);

		targetEnt = new Tank();
		targetEnt.setOwner(target);
		targetEnt.setId(20);
		targetEnt.setPosition(new Coords(1, 0));

		mechEnt = new BipedMech();
		mechEnt.setOwner(attacker);
		mechEnt.setId(35);
		mechEnt.setPosition(new Coords(0, 1));
		mechEnt.setWeight(200);

		entities = new Vector<Entity>();
		entities.add(attackerEnt);
		entities.add(targetEnt);
		entities.add(mechEnt);
		bot.game.setEntitiesVector(entities);

		club = new Mounted(attackerEnt, new MiscType());

		attackAction = new PushAttackAction(attackerEnt.getId(), targetEnt.getId(), targetEnt.getPosition());

	}

	/**
	 * Tear down after class.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	/**
	 * Sets the up.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@Before
	public void setUp() throws Exception
	{
	}

	/**
	 * Tear down.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@After
	public void tearDown() throws Exception
	{
	}

	/**
	 * Test to hit is impossible.
	 */
	@Test
	public void testToHitIsImpossible()
	{
		// should return null
		String actual = PushAttackAction.toHitIsImpossible(bot.game, attackerEnt, targetEnt);
		assertNull(actual);

		attackerEnt.setEvading(true);
		String expected = "Attacker is evading.";
		actual = PushAttackAction.toHitIsImpossible(bot.game, attackerEnt, targetEnt);
		assertEquals(expected, actual);
	}

	/**
	 * Test to hit.
	 */
	@Test
	public void testToHit()
	{
		// the attacker is a gunEnplacement so they have no arms to push
		ToHitData expected = new ToHitData(TargetRoll.IMPOSSIBLE, "Non-mechs can't push");
		ToHitData actual = PushAttackAction.toHit(bot.game, attackerEnt.getId(), targetEnt);
		assertEquals(expected, actual);

		// this attacker is a mech so they should be able to push a target
		expected = new ToHitData(TargetRoll.IMPOSSIBLE, "Target is not a mech");
		actual = PushAttackAction.toHit(bot.game, mechEnt.getId(), targetEnt);
		assertEquals(expected, actual);
		
		
	}

}
